package com.work.keep.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.activity.OnRecyclerItemClickListener;
import com.work.keep.entity.dao.Collection;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/26.
 */
public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.CollectionViewHolder> {
    private List<Collection> collections;
    private ImageLoader imageLoader;
    private List<Boolean> flags;
    private OnRecyclerItemClickListener onRecyclerItemClickListener;
    private View.OnClickListener onc;

    public CollectionAdapter(List<Collection> collections) {
        this.collections = collections;
        imageLoader = VolleySingleton.getInstance()._getImageLoader();
        flags = new ArrayList<>();
        for (int i = 0; i < collections.size(); i++) {
            flags.add(true);
        }
    }

    @Override
    public CollectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collection_pull_to_refresh, parent, false);
        return new CollectionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final CollectionViewHolder holder, final int position) {
        //增加在收藏页面对收藏的修改
        if (flags.get(position)) {
            holder.collectionIv.setImageResource(R.mipmap.collection_on);
            holder.collectionIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.collectionIv.setImageResource(R.mipmap.collection);
                    flags.set(position, false);
                }
            });
        } else {
            holder.collectionIv.setImageResource(R.mipmap.collection);
            holder.collectionIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.collectionIv.setImageResource(R.mipmap.collection_on);
                    flags.set(position, true);
                }
            });
        }

        holder.collectionContent.setText(collections.get(position).getContent());
        holder.collectionAvater.setImageUrl(collections.get(position).getAvater(), imageLoader);
        holder.collectionNI.setImageUrl(collections.get(position).getPhoto(), imageLoader);
        holder.collectionUsername.setText(collections.get(position).getUsername());
        //item点击事件
        onc = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRecyclerItemClickListener != null) {
                    onRecyclerItemClickListener.onItemClick(position);
                }
            }
        };
        //担心item 和 collectionIV点击事件冲突
        holder.collectionItemClickOne.setOnClickListener(onc);
        holder.collectionItemClickTwo.setOnClickListener(onc);
    }

    //返回flags的具体情况 判断是否有修改 此方法将写在收藏页面销毁的时候
    public List<Integer> getDeletePosition() {
        if (null == flags) {
            throw new NullPointerException("flags cannot be null");
        }
        List<Integer> deletePositions = new ArrayList<>();
        for (int i = 0; i < flags.size(); i++) {
            if (!flags.get(i)) {
                deletePositions.add(i);
            }
        }
        return deletePositions;
    }

    @Override
    public int getItemCount() {
        return collections.size();
    }


    class CollectionViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView collectionNI, collectionAvater;
        TextView collectionContent, collectionUsername;
        ImageView collectionIv;
        RelativeLayout collectionItemClickOne;
        LinearLayout collectionItemClickTwo;

        public CollectionViewHolder(View itemView) {
            super(itemView);
            collectionContent = (TextView) itemView.findViewById(R.id.collectionContent);
            collectionNI = (NetworkImageView) itemView.findViewById(R.id.collectionNI);
            collectionAvater = (NetworkImageView) itemView.findViewById(R.id.collectionAvater);
            collectionUsername = (TextView) itemView.findViewById(R.id.collectionUsername);
            collectionIv = (ImageView) itemView.findViewById(R.id.collectionIv);
            collectionItemClickOne = (RelativeLayout) itemView.findViewById(R.id.collectionItemClickOne);
            collectionItemClickTwo = (LinearLayout) itemView.findViewById(R.id.collectionItemClickTwo);
        }
    }
}
